﻿/*-----------------------------------------------
// File: FormationTransferState.cs
// Description: 
// Author: Shaobing	492057342@qq.com
-----------------------------------------------*/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using ProtoBuf.Message;

namespace Battle.View
{
    public class FormationTransferState : Fsm.FsmStateBase<BattleRoundState>
    {
        public BattleRoundCtrl battleRoundCtrl = null;

        BattlePanel battlePanel;
        BattleViewCtrl viewCtrl;

        public FormationTransferState(BattleRoundState state, BattleRoundCtrl ctrl) : base(state, ctrl)
        {
            battleRoundCtrl = ctrl;
        }

        public override void Enter()
        {
            viewCtrl = BattleManager.Instance.battleViewCtrl;
            var ids = viewCtrl.GetActorUids(BattleCamp.Attacker);
            for (int i = 0; i < ids.Count; ++i)
            {
                viewCtrl.GetBattleCharacter(ids[i]).SwitchState(ActorState.TransFormation);
            }
            battlePanel = BattleManager.Instance.battlePanel;
            battlePanel.ShowTransferPanel();
            CameraController.Instance.SwitchCamp();
        }


        public override void Update()
        {
            if (viewCtrl.transLeftTime > 0f)
            {
                viewCtrl.transLeftTime -= Time.deltaTime;
                if (viewCtrl.transLeftTime <= 0f)
                {
                    BattleManager.Instance.battleViewCtrl.SendConfirmTrans();
                    return;
                }
            }
        }

        public override void Leave()
        {
            viewCtrl.isFormationTransfered = true;
            CameraController.Instance.Ready();
            battlePanel.HideTransferPanel();
            foreach (var uid in BattleViewCtrl.GetInstance().GetActorUids(battleRoundCtrl.currentCamp)) {
                var bc = BattleViewCtrl.GetInstance().GetBattleCharacter(uid);
                if (bc.isAlive)
                {
                    bc.SwitchState(ActorState.AttackReady);
                }
                else {
                    bc.SwitchState(ActorState.Dead);
                }
            }
        }

        public void Transfer(TransForward forward) {
            var ids = viewCtrl.GetActorUids(BattleCamp.Attacker);
            for (int i = 0; i < ids.Count; ++i) {
                var bc = viewCtrl.GetBattleCharacter(ids[i]);
                var s = bc.GetState(ActorState.TransFormation) as BCTransFormationState;
                if (forward == TransForward.Forward)
                {
                    s.BeginMove(GetNextFPId(bc.FPId));
                }
                else {
                    s.BeginMove(GetPreFPId(bc.FPId));
                }
            }
        }

        int GetNextFPId(int fpId) {
            if (fpId == 3)
            {
                return 7;
            }
            else if (fpId == 7)
            {
                return 13;

            }
            else if (fpId == 9)
            {
                return 3;
            }
            else if (fpId == 13) {
                return 9;
            }
            return 0;
        }
        int GetPreFPId(int fpId) {
            if (fpId == 3)
            {
                return 9;
            }
            else if (fpId == 7)
            {
                return 3;

            }
            else if (fpId == 9)
            {
                return 13;
            }
            else if (fpId == 13)
            {
                return 7;
            }
            return 0;
        }

        public void Transfer(SCTransPosition tp)
        {
            for(int i = 0;i < tp.heroUids.Count;++i)
            {
                ((BCTransFormationState)(viewCtrl.GetBattleCharacter(tp.heroUids[i]).GetState(ActorState.TransFormation))).BeginMove(tp.heroFPIds[i]); ;
            }
        }
        public bool IsMoving()
        {
            var ids = viewCtrl.GetActorUids(BattleCamp.Attacker);
            for (int i = 0; i < ids.Count; ++i)
            {
                if(((BCTransFormationState)viewCtrl.GetBattleCharacter(ids[i]).GetState(ActorState.TransFormation)).isMoving)
                {
                    return true;
                }
            }
            return false;
        }
    }
}
